package com.luoran.bartalks.action;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.beetl.sql.core.engine.PageQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.luoran.bartalks.core.UserContext;
import com.luoran.bartalks.core.ext.Pageable;
import com.luoran.bartalks.entity.BaseInfo;
import com.luoran.bartalks.entity.biz.BtPlanTemplate;
import com.luoran.bartalks.entity.biz.BtTransportMode;
import com.luoran.bartalks.service.IBtCodingRuleService;
import com.luoran.bartalks.service.IBtPlanTemplateService;
import com.luoran.bartalks.service.IBtTransportModeService;
import com.luoran.bartalks.utils.EnumBillType;

@Controller
@RequestMapping("/transport")
public class BtTransportAction implements Pageable {

	@Autowired
	private IBtTransportModeService service;
	@Autowired
	private IBtCodingRuleService codingRuleService;
	@Autowired
	private IBtPlanTemplateService planTemplateService;

	private static final Logger log = LoggerFactory.getLogger(BtTransportAction.class);

	/**
	 * 运输方式列表页面
	 * 
	 * @return
	 */
	@RequestMapping
	public String queryTransport() {
		log.info("跳转到>>运输方式页面");
		return "basics/transport-list";
	}

	@Override
	public PageQuery<? extends BaseInfo> getQueryList(PageQuery<? extends BaseInfo> pageQuery) {
		log.info("获取运输方式列表数据,page:" + pageQuery.getPageNumber() + "size:" + pageQuery.getPageSize());
		@SuppressWarnings("unchecked")
		Map<String, String> map = (Map<String, String>) pageQuery.getParas();
		map.put("companyId", UserContext.get().getCompanyId());// 添加查询参数（公司ID）
		if (StringUtils.isEmpty(pageQuery.getOrderBy())) {
			pageQuery.setOrderBy("create_time DESC");
		}
		return service.getQueryList(pageQuery);
	}

	// 生成编码
	@RequestMapping("/generateCode")
	@ResponseBody
	public JSONObject gennerateCompanyCoding() {
		return codingRuleService.generateCoding(EnumBillType.TRANSPORTMETHOD, UserContext.get().getCompanyId());
	}

	@RequestMapping("addTransportMode")
	@ResponseBody
	public JSONObject addTransportMode(BtTransportMode transportMode) {
		log.info("新增运输方式信息》》");
		JSONObject res = new JSONObject();
		res = check(transportMode);
		if (!res.getBooleanValue("state")) {
			return res;
		}
		// 搜索是否已经存在
		BtTransportMode btTransportMode = service.getBtTransportModeByCode(transportMode.getTransportCode(),
				UserContext.get().getCompanyId());
		if (btTransportMode == null) {
			transportMode.setCreateTime(new Date());
			transportMode.setFounder(UserContext.get().getId());
			transportMode.setCompanyId(UserContext.get().getCompanyId());
			transportMode.setState(0);
			transportMode = service.addTransportMode(transportMode);
			res.put("msg", "新增成功！");
			res.put("state", true);
			return res;
		} else {
			res.put("msg", "编码“" + transportMode.getTransportCode() + "”已被其他引用！");
			res.put("state", true);
			return res;
		}
	}

	private JSONObject check(BtTransportMode transportMode) {
		JSONObject res = new JSONObject();
		if (StringUtils.isEmpty(transportMode.getTransportCode())) {
			res.put("msg", "编码不能为空！");
			res.put("state", false);
			return res;
		} else if (transportMode.getTransportCode().length() > 100) {
			res.put("msg", "编码长度限制为100！");
			res.put("state", false);
			return res;
		}
		if (StringUtils.isEmpty(transportMode.getTransportChineseName())) {
			res.put("msg", "中文名称不可以为空！");
			res.put("state", false);
			return res;
		} else if (transportMode.getTransportChineseName().length() > 100) {
			res.put("msg", "中文名称长度限制为100！");
			res.put("state", false);
			return res;
		}
		if (StringUtils.isEmpty(transportMode.getTransportEnglishName())) {
			res.put("msg", "英文名称不可以为空！");
			res.put("state", false);
			return res;
		} else if (transportMode.getTransportEnglishName().length() > 100) {
			res.put("msg", "英文名称长度限制为100！");
			res.put("state", false);
			return res;
		}

		res.put("state", true);
		return res;
	}

	// 删除
	@RequestMapping("/deleteTransportMode/{id}")
	@ResponseBody
	public JSONObject deleteTransportMode(@PathVariable("id") String id) {
		JSONObject res = new JSONObject();
		log.info("删除运输方式信息》》");
		service.deleteBtTransportModeById(id);
		res.put("msg", "删除成功！");
		res.put("state", true);
		return res;
	}

	// 获取信息
	@GetMapping("/getTransportMode/{id}")
	@ResponseBody
	public JSONObject getTransportMode(@PathVariable("id") String id) {
		log.info("根据Id 获取运输方式信息》》");
		JSONObject res = new JSONObject();
		BtTransportMode transportMode = service.getBtTransportMode(id);
		res.put("obj", transportMode);
		res.put("state", true);
		return res;
	}

	// 编辑
	@RequestMapping("editTransportMode")
	@ResponseBody
	public JSONObject editTemplateContainer(BtTransportMode transportMode) {
		log.info("编辑运输方式信息>>");
		JSONObject res = new JSONObject();
		res = check(transportMode);
		if (!res.getBooleanValue("state")) {
			return res;
		} else {
			service.editBtTransportMode(transportMode);
			res.put("msg", "修改成功！");
			res.put("state", true);
			return res;
		}
	}

	// 校验运输方式是否被计划模板引用.
	@RequestMapping("/checkTransportMode/{id}")
	@ResponseBody
	public JSONObject checkTransportMode(@PathVariable("id") String transportTypeId) {
		JSONObject result = new JSONObject();
		log.info("检测当前运输方式是否被引用>>");
		List<BtPlanTemplate> planTemplates = planTemplateService.getPlanTemplateByTransportTypeId(transportTypeId,
				UserContext.get().getCompanyId());

		if (planTemplates != null && planTemplates.size() != 0) {
			log.info("该运输方式已被引用<<");
			result.put("flag", true);
			result.put("msg", "当前运输方式已被引用,无法操作");
			return result;
		} else {
			log.info("该运输方式未被引用,可以操作");
			result.put("flag", false);
			return result;
		}

	}

}
